import random


def generate_captcha():
    # Generate a simple random 6-digit CAPTCHA
    return str(random.randint(100000, 999999))


def user_login():
    # Hardcoded username and password for validation
    valid_username = "username123"
    valid_password = "password123"

    attempts = 0

    while attempts < 3:
        # Generate and display CAPTCHA
        captcha = generate_captcha()
        print(f"CAPTCHA: {captcha}")

        # Get user input for username, password, and CAPTCHA
        input_username = input("Enter your username: ")
        input_password = input("Enter your password: ")
        input_captcha = input("Enter the CAPTCHA: ")

        # Validate the CAPTCHA before incrementing attempts
        if input_captcha == captcha:
            # Validate the username and password
            if input_username == valid_username and input_password == valid_password:
                print("Login successful!")
                break
            else:
                print("Invalid username or password. Please try again.")
        else:
            print("CAPTCHA incorrect. Please try again.")

        # Increment attempts only if CAPTCHA is correct
        attempts += 1

    if attempts == 3:
        print("Too many incorrect attempts. Exiting program.")
        raise SystemExit


# Call the login function
user_login()
